<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/JD/Public/css/goodsAdd.css">
    <link rel="stylesheet" href="/JD/Public/css/bootstrap.min.css">
    <script src="/JD/Public/js/jquery.js"></script>
</head>
<body>
<div class="by">
    <div class="goodsadd">
        <img src="/JD/Public/img/logo.jpg" alt="">
        <h1>商品修改</h1>
    </div>
    <div class="container box">
    <form id="13" method="post"  action="__CONTROLLER__/addAm"  enctype="multipart/form-data">
        <input type="text" name="id" id="id">
        <div class="form-group" >
            <label >商品名称</label>
            <input  class="form-control ww" id="name" name="goods_name"   required="required">
        </div>
        <div class="form-group">
            <label >品牌</label>
            <select name="goods_brand_id" id="f_goods_category_id">
                <option value="0">请选择</option>
            </select>
        </div>
        <div class="form-group">
            <label >商品数量</label>
            <input  class="form-control" id="goods_number" name="goods_number"  required="required">
        </div>
        <div class="form-group">
            <label >商品规格</label>
            <input  class="form-control" id="goods_weight" name="goods_weight" required="required">
        </div>
        <div class="form-group">
            <label >商品价格</label>
            <input  class="form-control" id="goods_price" name="goods_price"  required="required">
        </div>
        <div class="form-group">
            <label >产品介绍</label>
            <textarea class="form-control" id="goods_introduce" name="goods_introduce" rows="3" required="required" ></textarea>
        </div>
        <div class="form-group">
            <label >产品详情</label>
            <textarea class="form-control" id="goods_text" name="goods_text" rows="3" required="required" ></textarea>
        </div>
        <div class="form-group">
            <p>
                原图片
                <img id="img" src="" alt="">
            </p>
            <label >重新上传</label>



            <input type="file" name="goods_img">

        </div>

        <button type="submit" class="btn btn-default">保存修改</button>
    </form>
    </div>
</div>
<script>
    $(function(){

        var url=document.location.href;
        var id=getRequest(url).id;
        var ip=id;
        $("#id").val(id);
        $("#id").css("display","none");

        //请求后端接口，拿到所有分类信息
        $.get("__CONTROLLER__/getAm",function(res){
            var allCate=res;
            var list='';
            for(var i= 0;i<allCate.length;i++){
                list+='<option value="'+allCate[i].cat_id+'">'+allCate[i].cat_name+'</option>'
            }
            $("#f_goods_category_id option").first().after(list);
        })




        //获取所有的品牌显示到页面
        $.get("__CONTROLLER__/getGoods",function(res){
            var goods=res;





            for(var i=0;i<goods.length;i++){
                if(goods[i].id==id){
                    console.log(goods[i]);
                    $("#name").val(goods[i].goods_name);
                    $("#goods_number").val(goods[i].goods_number);
                    $("#goods_weight").val(goods[i].goods_weight);
                    $("#goods_price").val(goods[i].goods_price);
                    $("#goods_introduce").val(goods[i].goods_introduce);
                    $("#goods_text").val(goods[i].goods_text);
                    $("#img")[0].src="/JD/"+goods[i].goods_img;
                    $("#f_goods_category_id option")[goods[i].goods_brand_id].selected="selected";
                }
            }
        })
        function getRequest() {
            var url = window.location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for(var i = 0; i < strs.length; i ++) {

                    theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);

                }
            }
            return theRequest;
        }




    })
</script>
</body>
</html>